package si.yun.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import si.yun.entity.SyLoginRecord;
import si.yun.entity.SyUser;
import si.yun.service.LoginService;
import si.yun.utils.AddressUtils;
import si.yun.utils.JsonData;
import si.yun.utils.ipUtils;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @ClassName LoginWeb
 * @Author SXM
 * @Date 2021/07/28 14:09
 * @Version 1.0
 */
@RestController
@RequestMapping("/login")
public class LoginWeb {
    @Autowired
    private LoginService service;

    /*
     * @Author SXM
     * @Description //用户登录
     * @return si.yun.utils.JsonData
     * @Date 2021/7/28  19:15
     **/
    @RequestMapping("/selUser")
    private JsonData selUser(String userName, String passWord, HttpSession session){
        //int a = 1 / 0;
        //设置会话的生命周期
        //session.setMaxInactiveInterval(100);
        //System.out.println(userName+"=="+passWord);
        SyUser user=service.selUser(userName,passWord);
        //System.out.println(user);
        if(user!=null) {
            session.setAttribute("user", user);
            return JsonData.buildSuccess(user);
        }
        return JsonData.buildError("登录失败");
    }

    /*
     * @Author SXM
     * @Description //获取登录用户信息
     * @return si.yun.utils.JsonData
     * @Date 2021/7/29  17:04
     **/
    @RequestMapping("/selUserContent")
    private JsonData selUserContent(HttpSession session){
        SyUser user=(SyUser) session.getAttribute("user");
        return JsonData.buildSuccess(user);
    }


    /*
     * @Author SXM
     * @Description //图片上传接口
     * @return si.yun.utils.JsonData
     * @Date 2021/8/2  9:24
     **/
    @RequestMapping("/updimg")
    public JsonData updimg(){
        System.out.println("图片上传");
        return JsonData.buildSuccess(1);
    }

    /*
     * @Author SXM
     * @Description //账户设置
     * @return si.yun.utils.JsonData
     * @Date 2021/7/29  15:32
     **/
    @RequestMapping("/updUser")
    private JsonData updUser(int userId,String userName,String email,String npwd){
        //System.out.println(userName+"0110"+email+"0220"+npwd);
        int num=service.updUser(userId,userName,email,npwd);
        if(num>0){
            return JsonData.buildSuccess(1);
        }else{
            return JsonData.buildError("更新失败");
        }
    }


    /*
     * @Author SXM
     * @Description //发送短信验证码
     * @return
     * @Date 2021/8/1  19:07
     **/
    //@Autowired
    //public JavaMailSender mailSender;
    @RequestMapping("/selmail")
    public JsonData selmail(String toemail){
        //随机生成的验证码
        long random = Math.round(Math.random() * 1000000);
        System.out.println("随机生成的验证码"+random);
        String fromemail="2459530653@qq.com";
       /* if(toemail!=""){
            //新建邮箱对象
            SimpleMailMessage sm=new SimpleMailMessage();
            //设置值---发件人
            sm.setFrom(fromemail);
            //收件人
            sm.setTo(toemail);
            //邮件标题
            sm.setSubject("思云课堂验证信息");
            //邮件内容
            sm.setText("您好，您的验证码为"+random);
            //发送邮件
            mailSender.send(sm);
        }*/
        return JsonData.buildSuccess(random);
    };


    /*
     * @Author SXM
     * @Description //查询登录日志
     * @return si.yun.utils.JsonData
     * @Date 2021/7/30  14:22
     **/
   @RequestMapping("/selUserLog")
    public JsonData selUserLog(){
       List<SyLoginRecord> listlog=service.selUserLog();
       return JsonData.buildSuccess(listlog);
   }



}
